package com.sisyphus.mock.configs;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.sisyphus.mock.utils.Logger;
import com.yanzhenjie.andserver.annotation.Interceptor;
import com.yanzhenjie.andserver.framework.HandlerInterceptor;
import com.yanzhenjie.andserver.framework.handler.RequestHandler;
import com.yanzhenjie.andserver.http.HttpMethod;
import com.yanzhenjie.andserver.http.HttpRequest;
import com.yanzhenjie.andserver.http.HttpResponse;
import com.yanzhenjie.andserver.util.MultiValueMap;

@Interceptor
public class LoggerInterceptor implements HandlerInterceptor {

    @Override
    public boolean onIntercept(
            @NonNull HttpRequest request,
            @NonNull HttpResponse response,
            @NonNull RequestHandler handler) {

        String path = request.getPath();
        HttpMethod method = request.getMethod();
        MultiValueMap<String, String> valueMap = request.getParameter();
        Logger.i("Path: " + path);
        Logger.i("Method: " + method.value());
        Logger.i("Param: " + new Gson().toJson(valueMap));

        // 接口延时
        String delayMillisHeader = request.getHeader("delay-millis");
        if (delayMillisHeader != null) {
            try {
                Thread.sleep(Integer.parseInt(delayMillisHeader));
            } catch (Exception e) {

                e.printStackTrace();
            }
        }

        return false;
    }

}
